/********************************************
 * 4、设计一个算法，将按链式存储结构存储的线性表A，
 * A=(a1, a2, …an)逆转为A=(an,…, a2,a1)形式。
 * *******************************************/


#include<stdio.h>
#include<malloc.h>

struct NODE;
typedef struct NODE node;
struct NODE{int data;node* next;};

void reverse_list(node* h){
    node *p,*new_head;
    new_head = h;
    h = h->next;
    new_head->next = NULL;
    while(h){
        p = h;
        h = h->next;
        p->next = new_head->next;
        new_head->next = p;
    }
}

void print_linkedlist(const node* p){
    p = p->next;
    while(p){printf("%d ",p->data);p=p->next;}printf("\n");
}

int main(){
    node head;
    node* h = &head;
    node* p = h;
    int n = 10, i = 0;
    int A[10] = {1,2,3,4,5,6,7,8,9,10};

    for(i=0;i<n;i++){
        p->next = malloc(sizeof(node)); 
        p=p->next;
        p->data = A[i];
    }
    p->next=NULL;

    print_linkedlist(h);
    printf("Reverse the list. The list now is: \n");
    reverse_list(h);
    print_linkedlist(h);

    // clean-up
    while(h){p = h;h=h->next;free(p);}
    return 0;
}